<?php
namespace App\Service\H5;

class WorkhourService extends BaseService
{
    //数据列表
    public static function index($pas)
    {
        $db = self::dbConn(self::$dbConfigWorkHour)
            ->where('del', 0);
        $res = $db->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            $cntOnes = self::dbConn(self::$dbConfigWhCnt)
                ->where('del', 0)
                ->where('wh_id', $v->id)
                ->get();
            $cntAry = [];
            foreach ($cntOnes as $cntOne) {
                $cntAry[] = [
                    'id' => $cntOne->id,
                    'title' => $cntOne->cnt,
                ];
            }
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
                'cntList' => $cntAry,
            ];
        }
        return self::toback(0, '', $arys);
    }
    
    // 详情
    public static function getOne($id)
    {
        // 数据
        $res = (array) self::dbConn(self::$dbConfigWorkHour)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            $res = [
                'id' => 0, 'title' => '',
            ];
        }
        $arys['data'] = $res;
        return self::toback(0, '', $arys);
    }

   //新增
   public static function add($pas)
   {
       $res = self::dbConn(self::$dbConfigWorkHour)
           ->where('title', $pas['title'])
           ->first();
       if (!empty($res)) {
           return self::toback(-2, '存在同名数据');
       }
       $pas['addtime'] = $pas['uptime'] = toDate();
       $res = self::dbConn(self::$dbConfigWorkHour)->insertGetId($pas);
       if (!$res) {
           return self::toback(-1, 'error');
       }
       return self::toback();
   }

   //修改
   public static function modify($id, $pas)
   {
       $res = self::dbConn(self::$dbConfigWorkHour)
        ->where('id', $id)
        ->first();
       if (empty($res)) {
           return self::toback(-2, '数据不存在');
       }
       $res = self::dbConn(self::$dbConfigWorkHour)
           ->where('title', $pas['title'])
           ->where('id', '<>', $id)
           ->first();
       if (!empty($res)) {
           return self::toback(-2, '存在同名数据');
       }
       $pas['uptime'] = toDate();
       $res = self::dbConn(self::$dbConfigWorkHour)
           ->where('id', $id)
           ->update($pas);
       if (!$res) {
           return self::toback(-1, 'error');
       }
       return self::toback();
   }

   //删除
   public static function del($id)
   {
       $res = self::dbConn(self::$dbConfigWorkHour)->where('id', $id)->first();
       if (empty($res)) {
           return self::toback(-2, '数据不存在');
       }
       $res = self::dbConn(self::$dbConfigWorkHour)
           ->where('id', $id)
           ->update(['del' => 1, 'deltime' => toDate()]);
       if (!$res) {
           return self::toback(-1, 'error');
       }
       return self::toback();
   }

   //内容新增
   public static function cntAdd($pas)
   {
       $res = (array) self::dbConn(self::$dbConfigWhCnt)
        ->where('wh_id', $pas['wh_id'])
        ->where('cnt', $pas['cnt'])
        ->first();
       if (!empty($res)) {
           return self::toback(-2, '存在同名数据');
       }
       $pas['uptime'] = $pas['addtime'] = toDate();
       $res = self::dbConn(self::$dbConfigWhCnt)->insertGetId($pas);
       if (!$res) {
           return self::toback(-1, 'error');
       }
       return self::toback();
   }

   //内容修改
   public static function cntModify($id, $pas)
   {
        $res = (array) self::dbConn(self::$dbConfigWhCnt)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $res = (array) self::dbConn(self::$dbConfigWhCnt)
            ->where('id', '<>', $id)
            ->where('cnt', $pas['cnt'])
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在同名数据');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbConfigWhCnt)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
   }

   //内容删除
   public static function cntDel($id)
   {
        $res = (array) self::dbConn(self::$dbConfigWhCnt)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $pas['del'] = 1;
        $pas['deltime'] = toDate();
        $res = self::dbConn(self::$dbConfigWhCnt)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
   }
}